//
//  DetailCountDownCell.m
//  PPDL
//
//  Created by 王洋 on 2019/10/18.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "DetailCountDownCell.h"
#import "OYCountDownManager.h"

@implementation DetailCountDownCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

// xib创建
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        // 监听通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNotification) name:OYCountDownNotification object:nil];
    }
    return self;
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)fillCellWithModel:(DetailGoodInfoModel *)model {
    
    self.model = model;
    
}

- (void)countDownNotification {
    
    NSTimeInterval currentTimeStamp = [Utilities getCurrentServerTimestamp];
    
    NSInteger count;
    if (currentTimeStamp < self.model.itemStartTime) {
        
        count = (self.model.itemStartTime - currentTimeStamp)/1000;
        [self changeStatusDistanceToStart];
        
    } else if (currentTimeStamp > self.model.itemStartTime && currentTimeStamp < self.model.itemEndTime) {
        
        count = (self.model.itemEndTime - currentTimeStamp)/1000;
        [self changeStatusDistanceToEnd];
        
    } else {
        
        count = 0;
        [self changeShowStatusEnd];
    }

    self.itemHourLab.text = [NSString stringWithFormat:@" %02ld ",(long)count/3600];
    self.itemMinuteLab.text = [NSString stringWithFormat:@" %02ld ",(long)(count/60)%60];
    self.itemSecondLab.text = [NSString stringWithFormat:@" %02ld ",(long)count%60];
}


- (void)changeShowStatusEnd {

//    self.leaveTime.layer.backgroundColor = [UIColor lightGrayColor].CGColor;
    
    [self.itemIcon setImage:[UIImage imageNamed:@"alarm_clock_808080"]];
    
    self.itemStatusLab.textColor = UIColorHex(0x808080);
    self.itemStatusLab.text = @"拍场已结束";

    self.itemHourLab.hidden = YES;
    self.colonLab1.hidden = YES;
    self.itemMinuteLab.hidden = YES;
    self.colonLab2.hidden = YES;
    self.itemSecondLab.hidden = YES;
}


- (void)changeStatusDistanceToStart {
    
//    self.leaveTime.layer.backgroundColor = UIColorHex(0x219C00).CGColor;
    
    self.itemStatusLab.text = @"距开始";
    
    [self.itemIcon setImage:[UIImage imageNamed:@"alarm_clock"]];

    self.itemStatusLab.textColor = UIColorHex(0x219C00);
//    self.itemHourLab.layer.backgroundColor = UIColorHex(0x219C00).CGColor;
    self.colonLab1.textColor = UIColorHex(0x219C00);
//    self.itemMinuteLab.layer.backgroundColor = UIColorHex(0x219C00).CGColor;
    self.colonLab2.textColor = UIColorHex(0x219C00);
//    self.itemSecondLab.layer.backgroundColor = UIColorHex(0x219C00).CGColor;
    
    self.itemHourLab.hidden = NO;
    self.colonLab1.hidden = NO;
    self.itemMinuteLab.hidden = NO;
    self.colonLab2.hidden = NO;
    self.itemSecondLab.hidden = NO;
    
}

- (void)changeStatusDistanceToEnd {

    self.itemStatusLab.text = @"距结束";
    
    [self.itemIcon setImage:[UIImage imageNamed:@"alarm_clock_ff9090"]];
    
    self.itemStatusLab.textColor = UIColorHex(0xFF9000);
//    self.itemHourLab.layer.backgroundColor = UIColorHex(0xFF9000).CGColor;
    self.itemHourLab.backgroundColor = UIColorHex(0xFF9000);
    
    self.colonLab1.textColor = UIColorHex(0xFF9000);
//    self.itemMinuteLab.layer.backgroundColor = UIColorHex(0xFF9000).CGColor;
    self.itemMinuteLab.backgroundColor = UIColorHex(0xFF9000);

    self.colonLab2.textColor = UIColorHex(0xFF9000);
//    self.itemSecondLab.layer.backgroundColor = UIColorHex(0xFF9000).CGColor;
    self.itemSecondLab.backgroundColor = UIColorHex(0xFF9000);

    self.itemHourLab.hidden = NO;
    self.colonLab1.hidden = NO;
    self.itemMinuteLab.hidden = NO;
    self.colonLab2.hidden = NO;
    self.itemSecondLab.hidden = NO;
}

- (DetailGoodInfoModel *)model {
    if (!_model) {
        _model = [[DetailGoodInfoModel alloc] init];
    }
    return _model;
}
@end
